﻿/**
 * Create on 2012-2-3.
 */
package kit4j.tests.encode;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * 
 * @author 吴政
 *
 */
public class TestChineseCharacterCoding {

	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println("----generatorCharacterUsingString----");
		generatorCharacterUsingString();
		System.out.println();
		
		System.out.println("----generatorCharacterUsingCharCast----");
		generatorCharacterUsingCharCast();
		System.out.println();
		
		System.out.println("----generatorCharacterUsingCharsetDecode----");
		generatorCharacterUsingCharsetDecode();
		System.out.println();
		
	}
	
	static void generatorCharacterUsingString() {
		byte firstBit = (byte) 0xB0;
		byte secondBit = (byte) 0xA1;
		
		byte[] bytes = {firstBit, secondBit};
		
		try {
			System.out.println(new String(bytes, "GBK"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	static void generatorCharacterUsingCharCast() {
		System.out.println((char) 0xB0A1);
	}
	
	static void generatorCharacterUsingCharsetDecode() {
		byte firstBit = (byte) 0xB0;
		byte secondBit = (byte) 0xA1;
		
		ByteBuffer buf = ByteBuffer.allocate(2);
		
		buf.put(firstBit).put(secondBit);
		
		buf.rewind();
		System.out.println("buf: 0x" + Integer.toHexString(buf.getChar()));
		
		buf.rewind();
		System.out.println(Charset.forName("GBK").decode(buf).get());
	}

}
